package operation;

import book.BookShelf;
import book.Book;
import user.User;

import java.util.Scanner;

public class DelOperation implements IFind,Operation{
    @Override
    public int find(String name, BookShelf bookShelf) {
        //获取书架
        Book[] books = bookShelf.getBookShelf();
        //获取有效元素个数
        int size = bookShelf.getSize();

        for(int i = 0; i < size; i++) {
            if(books[i].getTitle().equals(name)) {
                //找到就返回下标
                return i;
            }
        }
        //找不到就返回-1，统一处理
        return -1;
    }

    @Override
    public void work(BookShelf bookShelf) {
        if(bookShelf.getSize()==0) {
            System.out.println("书架为空，无法执行删除操作！");
        }
        Book[] books = bookShelf.getBookShelf();
        int size = bookShelf.getSize();

        System.out.println("请输入您要删除的书名");
        Scanner in = new Scanner(System.in);
        String name = in.nextLine();
        int ret = find(name,bookShelf);
        if(ret == -1) {
            System.out.println("未找到您需要删除的书");
        } else {
            //循环覆盖
            for(int i = ret; i < books.length-1; i++) {
                books[i] = books[i+1];
            }
            //有效元素减一
            bookShelf.setSize(size-1);
            System.out.println("删除成功！");
        }
    }

}